#include "ys_cap_setup.h"
#include "ui_ys_cap_setup.h"
#include "pub_var.h"
#include <QString>
#include <QMessageBox>
#include <QDebug>
#include <QSettings>

extern QList<QCameraInfo> cameraList;

Ys_cap_setup::Ys_cap_setup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Ys_cap_setup)
{    
    ui->setupUi(this);
    initialized = false;

    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 50; j++)
        {
            modes[i][j] = -1;
        }
    }

    if( 0 == cameraList.size())
    {
        qDebug()<<"设置，没有找到摄像头！\n";
        QMessageBox::information(this,"","没有找到摄像头！");
    }

    QSettings setIni("clin.ini", QSettings::IniFormat);

    QRect rct = setIni.value("windowState/ys_cam_setup").toRect();
    this->setGeometry(rct);

    QString cur_cam_name = setIni.value("camera/defaultCamera").toString();
    //int cur_cam_sn = setIni.value("camera/defaultId").toInt();
    QString camera_mode = setIni.value("camera/"+cur_cam_name+"/defaultMode").toString();

    state1 = "默认摄像头：";
    state2 = " , 当前工作模式：mode ";
    state3 ="";

    for(int i = 0; i < cameraList.size(); i++)
    {
        ui->cbCamera->addItem(cameraList[i].description());
        state3 = state3 + "摄像头"+QString::number(i)+"设备名称:"+cameraList.at(i).description();//list.at(i).deviceName();
        camera= new QCamera(cameraList.at(i).deviceName().toLatin1().data());//摄像头

        //启动摄像头。摄像头被占用无法启动，但可以返回参数
        camera->start();

        QList<QCameraViewfinderSettings > ViewSets = camera->supportedViewfinderSettings();//获取摄像头支持的分辨率、帧率等参数
        state3 = state3 + "，可选工作模式：\n";//+ QString::number(ViewSets.length())+" 种 \n";
        int m = 0;
        int n = 0;

        foreach (QCameraViewfinderSettings ViewSet, ViewSets) {

            QString res;
            QString fmt;
            QVideoFrame::PixelFormat pfmt = ViewSet.pixelFormat();
            QSize sz = ViewSet.resolution();
            if(QVideoFrame::Format_Jpeg == pfmt
                    || QVideoFrame::Format_BGR24 == pfmt
                    || QVideoFrame::Format_YUYV == pfmt)
            {
                modes[i][n++] = m;
                if(QVideoFrame::Format_Jpeg == pfmt)fmt = "Format_Jpeg";
                if(QVideoFrame::Format_BGR24 == pfmt)fmt = "Format_BGR24";
                if(QVideoFrame::Format_YUYV == pfmt)fmt = "Format_YUYV";

                res = QString::number(sz.width())+"*"+QString::number(sz.height());
                state3 = state3 +"mode " + QString::number(m++) + ", resolution "+ res
                        + ", Format = " + fmt +"\n";
            }
            else
            {
                m++;
            }
        }
        modes[i][n] = -1;
        state3 = state3 + "\n\n";

        camera->stop();
        camera->unload();
        delete camera;
        camera = nullptr;

    }

    int pos = ui->cbCamera->findText(cur_cam_name, Qt::MatchExactly);

    if(pos < 0)
    {
        //如果默认设备在当前列表不存在
        qDebug()<<" default name is not in current device list.";
        cur_cam_name = ui->cbCamera->currentText();
        camera_mode = "0";
    }

    qDebug()<<"current set is : "<<state1 + cur_cam_name + state2 +camera_mode<<"\n";
    QString s = state1 + cur_cam_name + state2 +camera_mode +"\n\n" +state3;
    ui->textEdit->setText(s);

    getResList(cur_cam_name);

    qDebug()<<"cur_cam_name " << cur_cam_name<<"\n";
    qDebug()<<"camera_mode " << camera_mode<<"\n";

    ui->cbCamera->setCurrentText(cur_cam_name);
    ui->cbResolution->setCurrentText(camera_mode);

    initialized = true;

    qDebug()<<"end of Ys_cap_setup initialize.\n";
}

Ys_cap_setup::~Ys_cap_setup()
{
    //delete camera;
    delete ui;
}

void Ys_cap_setup::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/ys_cam_setup", QVariant(this->geometry()));
}

void Ys_cap_setup::on_pb_save_clicked()
{
    QString cur_cam_name = ui->cbCamera->currentText();
    QString camera_mode = ui->cbResolution->currentText();

    int cam_sn = -1; //设备序号

    if(camera_mode.isEmpty())
    {
        QMessageBox::information(this,"","无效工作模式！");
        return;
    }

    QSettings setIni("clin.ini", QSettings::IniFormat);

    for(int i = 0; i < cameraList.size(); i++)
    {
        //ui->cbb_cam_id->addItem(QString::number(i-1));
        if(cameraList[i].description() == cur_cam_name)
        {
            cam_sn = i;
        }
    }

    setIni.setValue("camera/"+cur_cam_name+"/defaultMode", camera_mode);
    setIni.setValue("camera/defaultCamera", cur_cam_name);
    setIni.setValue("camera/defaultId", cam_sn);

    QMessageBox::information(this,"","摄像头默认工作模式保存成功!");
    ui->textEdit->clear();

    QString s = state1 + cur_cam_name + state2 + camera_mode +"\n\n" +state3;
    ui->textEdit->setText(s);

}

void Ys_cap_setup::on_pb_exit_clicked()
{
    this->close();
}

void Ys_cap_setup::on_cbCamera_currentTextChanged(const QString &arg1)
{
    qDebug()<<"void Ys_cap_setup::on_cbCamera_currentTextChanged(const QString &arg1)\n";
    qDebug()<<"initialized : "<<initialized<<"\n";
    if(!initialized)
    {
        qDebug()<<"no need to do anything. \n";
        return;
    }

    qDebug()<<"will get do getResList with " + arg1 + "\n";
    getResList(arg1);

}

void Ys_cap_setup::getResList(QString arg)
{
    qDebug()<<"void Ys_cap_setup::getResList(QString arg)\n";
    qDebug()<< "camera name : " << arg <<"\n";
    qDebug()<< "cameraList.size() : " << QString::number(cameraList.size()) <<"\n";

    int row = -1;
    for(int i = 0; i < cameraList.size(); i++)
    {
        qDebug()<< "cameraList[i].description() : " << cameraList[i].description() <<"\n";
        if(cameraList[i].description() == arg)
        {
            row = i;
            break;
        }
    }

    ui->cbResolution->clear();
    for( int f =0; f<50; f++)
    {
        if(modes[row][f] == -1)
        {
            break;
        }
        qDebug()<< "resolution id : " << QString::number(modes[row][f]) <<"\n";
        ui->cbResolution->addItem(QString::number(modes[row][f]));
    }
    qDebug()<<"end of getResList.\n";

}

